package com.quxiu.system.controller;

import java.util.List;
import javax.annotation.Resource;

import com.quxiu.common.core.utils.StringUtils;
import com.quxiu.common.core.utils.security.SecretUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quxiu.common.log.annotation.Log;
import com.quxiu.common.log.enums.BusinessType;
import com.quxiu.common.security.annotation.RequiresPermissions;
import com.quxiu.system.domain.SysApp;
import com.quxiu.system.service.ISysAppService;
import com.quxiu.common.core.web.controller.BaseController;
import com.quxiu.common.core.web.domain.AjaxResult;
import com.quxiu.common.core.web.page.TableDataInfo;

/**
 * 应用Controller
 *
 * @author GaoYang
 * @date 2024-01-19
 */
@RestController
@RequestMapping("/app")
public class SysAppController extends BaseController {
    @Resource
    private ISysAppService sysAppService;

    /**
     * 查询应用列表
     */
    @RequiresPermissions("system:app:list")
    @GetMapping("/list")
    public TableDataInfo list(SysApp sysApp) {
        startPage();
        List<SysApp> list = sysAppService.selectSysAppList(sysApp);
        return getDataTable(list);
    }

    /**
     * 获取应用详细信息
     */
    @RequiresPermissions("system:app:query")
    @GetMapping(value = "/{appId}")
    public AjaxResult getInfo(@PathVariable("appId") String appId) {
        return success(sysAppService.selectSysAppByAppId(appId));
    }

    /**
     * 新增应用
     */
    @RequiresPermissions("system:app:add")
    @Log(title = "应用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysApp sysApp) {
        if (!SecretUtil.testRsaKey(sysApp.getPublicKey(), sysApp.getPrivateKey())) {
            return error("填写的公钥与私钥是无效的！");
        }
        return toAjax(sysAppService.insertSysApp(sysApp));
    }

    /**
     * 修改应用
     */
    @RequiresPermissions("system:app:edit")
    @Log(title = "应用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysApp sysApp) {
        if (!SecretUtil.testRsaKey(sysApp.getPublicKey(), sysApp.getPrivateKey())) {
            return error("填写的公钥与私钥是无效的！");
        }
        return toAjax(sysAppService.updateSysApp(sysApp));
    }

    /**
     * 生成应用密钥对
     */
    @GetMapping("/generateKeyPair")
    public AjaxResult generateKeyPair() {
        return success(SecretUtil.generateRsaKeyHex());
    }

    /**
     * 删除应用
     */
    @RequiresPermissions("system:app:remove")
    @Log(title = "应用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{appIds}")
    public AjaxResult remove(@PathVariable String[] appIds) {
        return toAjax(sysAppService.deleteSysAppByAppIds(appIds));
    }

    /**
     * 全量推送用户数据
     */
    @RequiresPermissions("system:app:pushData")
    @Log(title = "全量推送用户数据", businessType = BusinessType.OTHER)
    @PostMapping(value = "/pushUserData")
    public AjaxResult pushUserData(@RequestBody String appId) {
        if (StringUtils.isBlank(appId)) {
            return error("appId不能为空");
        }
        sysAppService.pushUserDataAll(appId);
        return success();
    }

    /**
     * 全量推送部门数据
     */
    @RequiresPermissions("system:app:pushData")
    @Log(title = "全量推送部门数据", businessType = BusinessType.OTHER)
    @PostMapping(value = "/pushDeptData")
    public AjaxResult pushDeptData(@RequestBody String appId) {
        if (StringUtils.isBlank(appId)) {
            return error("appId不能为空");
        }
        sysAppService.pushDeptDataAll(appId);
        return success();
    }
}
